<template>
  <div>
    <!-- dialog弹出框 -->
    <el-dialog
      title="审核信息"
      :visible.sync="show"
      width="50%"
      :before-close="closeDialog"
    >
      <!-- from表单 -->
      <div class="from-infomations">
        <el-form ref="form" label-position="left" :model="sendInfo" label-width="8rem">
          <el-form-item label="姓名">
            {{ userInfo.userName }}
          </el-form-item>
          <el-form-item label="工号">
            {{ userInfo.userId }}
          </el-form-item>
          <el-form-item label="当前状态">
            <template>
              <el-radio-group v-model="sendEndScore">
                <el-radio :label="1">非一票否决</el-radio>
                <el-radio :label="2">一票否决</el-radio>
              </el-radio-group>
            </template>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog()">取 消</el-button>
        <el-button type="primary" @click="sendDialog()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getAllInfo } from '../../api/user.js'
import { getSendInfo, getDelSend, getAddSend } from '../../api/send.js'
import { isOpenSendTime } from '../../utils/isOpenSendTime.js'
export default {
  props: {
    // 状态获取
    getStatusType: {
      type: Array,
      default: () => {}
    }
  },
  data() {
    return {
      // 获取全部数据存储位置
      show: false,
      sendInfo: {
        sendId: '',
        sendTitle: '',
        sendInfo: '',
        sendFlistId: '',
        sendSlistId: '',
        sendTlistId: '',
        sendUserId: '',
        sendStatus: '',
        sendScore: '',
        sendImages: '',
        sendGiveUserId: ''
      },
      userInfo: {
        userId: '',
        userPwd: '',
        userType: '',
        userInfo: {
          userBranch: '',
          userId: '',
          userName: '',
          userReconsider: '',
          userScore: ''
        }
      },
      sendEndScore: '',
      // 代码数据存储位置
      sendStatus: ''
    }
  },
  watch: {
  },
  created() {
    // dialog信息初始化
    this.createDialogStatus()
  },
  methods: {
    // 全部调用方法
    // 展示dialog
    showDialog(data) {
      this.show = true
      var that = this
      //  获取用户信息
      that.statusType = data
      new Promise(function(resolve, reject) {
        // 根据用户id获取用户信息
        var userId = that.statusType.userId
        getAllInfo(userId).then((res) => {
          resolve(res.obj)
        })
      }).then(data => {
        that.userInfo.userId = data.userId
        that.userInfo.userType = data.userType
        that.userInfo.userName = data.userinfo.userName
        that.userInfo.userBranch = data.userinfo.userBranch
        that.userInfo.userReconsider = data.userinfo.userReconsider
        that.userInfo.userScore = data.userinfo.userScore
        // 根据sendId判断send表
        var sendId = that.statusType.sendId
        if (sendId === 'none') {
          // 设置数据，保存当前状态
          that.sendStatus = 0
        } else {
          // 设置数据，保存当前状态
          that.sendStatus = 1
          // send表中有数据，根据sendId查找send表中的信息
          that.findSend(sendId)
        }
      }).then(() => {
        if (that.sendStatus === 0) {
          that.sendEndScore = 1
        } else if (that.sendStatus === 1) {
          that.sendEndScore = 2
        }
      })
    },
    // 根据sendId查询send表中的信息
    findSend(sendId) {
      var that = this
      new Promise(function(resolve, reject) {
        getSendInfo(sendId).then((res) => {
          resolve(res.obj)
        })
      }).then((data) => {
        that.sendInfo = data
        if (data.sendScore !== null) { that.sendInfo.sendScore = data.sendScore }
      })
    },
    // 初次提交，添加信息
    addSend() {
      var that = this
      var send = {}
      send.sendScore = that.sendToScore
      send.sendUserId = that.getStatusType.userId
      send.sendFlistId = sessionStorage.getItem('flist')
      send.sendSlistId = sessionStorage.getItem('slist')
      send.sendSemseterId = sessionStorage.getItem('semId')
      send.sendStatus = '1'
      send.sendTlistId = sessionStorage.getItem('tlist')
      send.sendGiveUserId = sessionStorage.getItem('userId')
      // 添加send表中的信息
      new Promise(function(resolve, reject) {
        getAddSend(send).then((res) => {
          resolve(res.result)
        })
      }).then((data) => {
        if (data === true) {
          that.show = false
          that.$message({
            message: '添加成功',
            type: 'success'
          })
          top.location.reload()
          that.sendEndScore = ''
        }
      })
    },
    // 再次提交，删除信息
    updateSend() {
      var that = this
      that.sendInfo.sendGiveUserId = sessionStorage.getItem('userId')
      // 根据sendId修改信息
      new Promise(function(resolve, reject) {
        getDelSend(that.statusType.sendId).then((res) => {
          resolve(res.obj)
        })
      }).then((data) => {
        if (data === null) {
          that.show = false
          that.$message({
            message: '删除成功',
            type: 'success'
          })
          that.sendEndScore = ''
          top.location.reload()
        }
      })
    },

    // 页面逻辑方法
    // dialog信息初始化
    createDialogStatus() {
      this.show = false
    },

    // 关闭dialog
    closeDialog() {
      this.$confirm('是否确定取消', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.show = false
        this.$message({
          type: 'success',
          message: '取消审核成功'
        })
      }).catch(() => {})
    },
    // 提交dialog
    sendDialog() {
      var that = this
      var sendEndScore = that.sendEndScore
      // 看下是否在开放时间内
      isOpenSendTime().then((res) => {
        that.timeType = res
        if (that.timeType === 1) {
          if (sendEndScore === '') {
            that.$message({
              message: '请选择状态后再提交',
              type: 'warning'
            })
          } else {
            if (that.sendEndScore === 2 && that.sendInfo.sendScore < -500.0) {
              that.$message({
                message: '当前用户已被一票否决，请勿再次提交',
                type: 'warning'
              })
            } else if (that.sendEndScore === 1 && that.sendStatus === 0) {
              that.$message({
                message: '当前用户非被一票否决，请勿再次提交',
                type: 'warning'
              })
            } else {
              if (that.sendInfo.sendScore !== '' || that.sendInfo.sendScore !== null || that.sendInfo.sendScore !== undefined) {
                new Promise(function(resolve, reject) {
                  if (sendEndScore === 2) {
                    if (that.sendInfo.sendScore < -500.0) {
                      that.sendToScore = that.sendInfo.sendScore
                    }
                    that.sendToScore = that.sendInfo.sendScore - 999.0
                  } else {
                    if (that.sendInfo.sendScore < -500.0) {
                      that.sendToScore = that.sendInfo.sendScore + 999.0
                    } else {
                      that.sendToScore = -that.sendInfo.sendScore
                    }
                  }
                  resolve()
                }).then(() => {
                  new Promise(function(resolve, reject) {
                    that.$confirm('是否确定提交', '提示', {
                      confirmButtonText: '确定',
                      cancelButtonText: '取消',
                      type: 'warning'
                    }).then(() => {
                      that.$message('正在提交中')
                      resolve(1)
                    }).catch(() => {
                      resolve(2)
                    })
                  }).then((data) => {
                    if (data === 1) {
                      if (that.getStatusType.sendId === 'none') {
                      // 初次提交，添加信息
                        that.addSend()
                      } else if (that.getStatusType.sendId !== 'none') {
                      // 再次提交，修改信息
                        that.updateSend()
                      } else {
                        that.$message({
                          message: '当前服务器繁忙，请稍后再试',
                          type: 'warning'
                        })
                      }
                    }
                  })
                })
              } else if (sendEndScore === 2) {
                that.sendToScore = -999
                new Promise(function(resolve, reject) {
                  that.$confirm('是否确定提交', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                  }).then(() => {
                    that.$message('正在提交中')
                    resolve()
                  }).catch(() => {
                    resolve()
                  })
                }).then(() => {
                  if (that.getStatusType.sendId === 'none') {
                  // 初次提交，添加信息
                    that.addSend()
                  } else if (that.getStatusType.sendId !== 'none') {
                  // 再次提交，修改信息
                    that.updateSend()
                  } else {
                    that.$message({
                      message: '当前服务器繁忙，请稍后再试',
                      type: 'warning'
                    })
                  }
                })
              }
            }
          }
        } else if (that.timeType === 2) {
          that.$message({
            message: '未到开放时间',
            type: 'warning'
          })
        } else if (that.timeType === 3) {
          that.$message({
            message: '已过开放时间',
            type: 'warning'
          })
        } else {
          that.$message({
            message: '系统繁忙请联系管理员',
            type: 'warning'
          })
        }
      })
    }
  }
}
</script>
